﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.BusinessEntity.YunZhiJia;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.ConstrainedExecution;
using System.Security.Cryptography;
using System.Text;
namespace LiMinCRMPlugin
{
    /// <summary>
    /// 销售订单老变更单审核插件
    /// </summary>
    /// <remarks>
    /// 功能： 销售订单老变更单审核后同步辅助单位数量
    /// 创建人：mey
    /// 创建日期：2024.8.2
    /// 修改信息（1）：修改人，修改日期，修改内容
    /// </remarks>
    /// 
    [Description("销售订单老变更单审核后同步数量到销售订单")]
    [HotUpdate]
    public class SaleOrderChangeServer : AbstractOperationServicePlugIn
    {
        //OnPreparePropertys 数据加载前，确保需要的属性被加载
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);

            //编码
            e.FieldKeys.Add("FBillNo");
            e.FieldKeys.Add("FORDERBILLID");//订单内码
            e.FieldKeys.Add("FORDERBILLNO");//订单编码
            e.FieldKeys.Add("FORDERENTRYID");//订单明细内码
            e.FieldKeys.Add("FOQty");//原销售数量
            e.FieldKeys.Add("FNQty");//新销售数量
            e.FieldKeys.Add("F_QLTK_FQty");//原辅助数量
            e.FieldKeys.Add("F_QLTK_NQty");//新辅助数量
        }

        /// <summary>
        /// 事务结束后事件
        /// </summary>
        /// <param name="e"></param>
        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);

            //读取全部的单据,for循环,转换成DynamicObject类型
            foreach (DynamicObject entity in e.DataEntitys)
            {

                string FID = Convert.ToString(entity["Id"]);//内码                      
                string Number = entity["BillNo"].ToString();//编码
                   

                //明细
                DynamicObjectCollection FEntity = entity["SaleOrderChangeEntry"] as DynamicObjectCollection;
               
                foreach (DynamicObject Fe in FEntity)
                {
                    string FORDERBILLID = Convert.ToString(Fe["ORDERBILLID"]);//订单内码                      
                    string FORDERBILLNO = Fe["ORDERBILLNO"].ToString();//订单编码
                    string FORDERENTRYID = Convert.ToString(Fe["ORDERENTRYID"]);//订单明细内码                      
                    decimal FOQty = Convert.ToDecimal(Fe["OQty"]);//原销售数量
                    decimal FNQty = Convert.ToDecimal(Fe["NQty"]);//新销售数量               

                    if (FOQty != FNQty && FORDERENTRYID!="" && FORDERBILLID!="")//数量变更的情况下
                    {
                        decimal F_QLTK_NQty = Convert.ToDecimal(Fe["F_QLTK_NQty"]);//新辅助数量  
                        string str = string.Format(@"/*dialect*/ update T_SAL_ORDERENTRY set F_RDLV_Qty_tzk="+ F_QLTK_NQty + " where fid='"+ FORDERBILLID + "' and Fentryid='"+ FORDERENTRYID + "'");
                   
                       DBServiceHelper.Execute(this.Context, str);
                    }
                }

            }
        }


    }
}
